Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CakeNumber::fromReadableSize() and Validation::filesize() #850

Merged
merged 2 commits into from Sep 18, 2012

Conversation

ceeram
Copy link
Contributor

@ceeram ceeram commented Sep 17, 2012

I still had this hanging around

if (substr($size, -1) == 'B' && ctype_digit(substr($size, 0, strlen($size) - 1))) {
return $size * 1;
}
throw new CakeException(__d('cake_dev', 'No unit type.'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a default return? throwing an exception sounds a bit too much for view related code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default return would be a function param

@markstory
Copy link
Member

I think this is a useful change and having additional basic validation methods for file uploads should make handling file uploads simpler for users.

$units = array('KB', 'MB', 'GB', 'TB', 'PB');
foreach ($units as $i => $unit) {
if ($unit == substr($size, -2)) {
return $size * pow(1024, $i + 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't remove the unit from $size?

Btw, to simplify a bit, you could get the last 2 chars outside, once size never changes. Also, you could override the foreach using array_search + if.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrbasso its merge din already like this, but this is what you had in mind?: 90c32ad

thx for the pointer

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ceeram The array_search portion looks good. But the multiplication point still kind of weird to me. For example, if $size is 10MB, you will have '10MB' * pow(1024, 1 + 1). Seems it is working, but is kind of weird to do math with 'MB' in the variable.

Btw, to avoid a issue in the future, what you think in use strtoupper in the unit? It will allow '10mb' as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, seems odd to calcaluate with MB in the strings, pow() converts all to numeric, but i think its clearer if i take it out.
Also will make it strtoupper

@jrbasso
Copy link
Member

jrbasso commented Sep 17, 2012

+1 for this change.

markstory added a commit that referenced this pull request Sep 18, 2012
Add CakeNumber::fromReadableSize() and Validation::filesize()
@markstory markstory merged commit 07c5102 into cakephp:2.3 Sep 18, 2012
@ceeram ceeram mentioned this pull request Sep 18, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants